package com.nb.hdj.testCase.consultModule;
import com.nb.hdj.baseClass.SeleniumUtil;
import com.nb.hdj.util.TestCaseRetry;
import com.nb.hdj.util.YamlReadUtils;
import org.openqa.selenium.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Description: TODO
 * @Author: hedongjia
 * @Date: 2021/11/15 下午8:47
 * @Version: v1.0
 */
public class InsertConsultCase extends ConsultCaseBase{
    private static final Logger logger = LoggerFactory.getLogger(InsertConsultCase.class);
//    YamlReadUtils yamlReadUtils=new YamlReadUtils();
//    SeleniumUtil seleniumUtil=new SeleniumUtil();
//    public Map<String, String> consultMap=yamlReadUtils.getMapValueByKey("Consult", "htconsult.yaml");
    String menuElement=consultMap.get("menuElement");
    String titleElement=consultMap.get("titleElement");
    String insertConsultElement=consultMap.get("insertConsultElement");


    public boolean insertConsult(WebDriver driver,String title,String author,String introduce,String content){
        boolean insertFlag=false;
        try {
            //点击内容管理主菜单
            if(seleniumUtil.elementClick(driver,By.xpath(menuElement))){
//               String elementText = seleniumUtil.getElementText(driver, By.xpath(titleElement));
//                System.out.println(elementText);
                //点击资讯文章子菜单
                seleniumUtil.elementClick(driver,By.xpath(consultMap.get("menuConsultElement")));
                seleniumUtil.sleep(1500);
                if (seleniumUtil.elementTextIsContains(driver, By.xpath(titleElement), consultMap.get("consultTitle"))) {
                    seleniumUtil.elementClick(driver, By.xpath(insertConsultElement));
                    seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(consultMap.get("insertConsultTitleElement")),title);
                    seleniumUtil.elementClick(driver,By.xpath(consultMap.get("insertConsultClassDownElement")));
                    //点击列表里最后一个元素
                    Thread.sleep(500);
                    seleniumUtil.clickLastLiElement(driver,consultMap.get("insertConsultClassUlElement"), consultMap.get("insertConsultClassliElement"));
                    //driver.findElement(By.xpath("(//ul[@class='el-scrollbar__view el-select-dropdown__list']/li[@class='el-select-dropdown__item'])[last()]/span")).click();
//                    List<WebElement> elements = driver.findElements(By.xpath("(//ul[@class='el-scrollbar__view el-select-dropdown__list']/li[@class='el-select-dropdown__item'])"));
//                    for (WebElement element : elements) {
//                        System.out.println(element.getText());
//                    }
                    //List<String> liForUlValues = seleniumUtil.getLiForUlValues(driver,consultMap.get("insertConsultClassUlElement"), consultMap.get("insertConsultClassliElement"));
                    seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(consultMap.get("insertConsultAuthorElement")),author);
                    Thread.sleep(500);
                    seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(consultMap.get("insertConsultIntroduceElement")),introduce);
                    //打开富文本
                    seleniumUtil.elementClick(driver,By.xpath(consultMap.get("insertConsultContentElement")));
                    seleniumUtil.switchToIframeById(driver,consultMap.get("insertConsultContentIframeElement"));
                    Thread.sleep(500);
                    seleniumUtil.elementClickAndClearAndInput(driver,By.id(consultMap.get("insertConsultContentBodyElement")),content);
                    seleniumUtil.exitIframe(driver);
                    Thread.sleep(500);
                    seleniumUtil.elementClick(driver,By.xpath(consultMap.get("insertConsultContentSaveElement")));
                    seleniumUtil.elementSendKeys(driver, By.name("file"), consultMap.get("insertConsultPicUrlElement"));
                    //上传后因服务器问题 等待5秒 太坑了啊
                    Thread.sleep(3000);
                    //暂时不插入pdf
                    //seleniumUtil.elementClick(driver,By.xpath(consultMap.get("insertConsultPdfElement")));
                    //Thread.sleep(500);
                    //seleniumUtil.autoUploadFile("/Users/dongjia/Desktop/测试专用pdf.pdf");
                    //元素可能为loading覆盖 等待loading元素消失
                    try{
                        seleniumUtil.elementClick(driver, By.xpath(consultMap.get("insertConsultSaveElement")));
                    }catch (ElementClickInterceptedException e){
                        logger.info("loading元素覆盖了当前元素");
                        Thread.sleep(5000);
                        return false;
                    }
                    insertFlag = seleniumUtil.elementTextIsContains(driver, By.xpath(consultMap.get("insertConsultSuccessTitleElement")), "自动化");
                }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
        return insertFlag;
    }
}
